<?php

namespace Home\Controller;

use Home\Core\CommandSDK;

class DeviceGroupController extends BaseController
{
    /**
     * 获取资源组列表
     */
    public function device_group()
    {
        $param = I('get.');
        $param['restype']= 0; //获取资源组
        $param['parentid']= 0;
        $cookie = base64_decode($_COOKIE['user_info']);
        parse_str($cookie);
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ResourcesLists, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $this->assign('companyname', $companyname);
        $this->assign('array', $array);
        $this->display();
    }
    /**
     * 获取资源列表
     */
    public function getResourcesList()
    {
        $param['parentid'] = I('post.parentid');
        $param['restype']= 1; //获取资源
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ResourcesLists, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $ability='';
        foreach ($array['resources'] as $key => $value) {
            $abilityArr = [];
            if ($value['ability'] & 0x00000001) {
                $ability = '主码流,';
                $array['resources'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000002) {
                $ability .= "子码流,";
                $array['resources'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000004) {
                $ability .= "云台,";
                $array['resources'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000008) {
                $ability .= "语音,";
                $array['resources'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000010) {
                $ability .= "对讲,";
                $array['resources'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000020) {
                $ability .= "WIFI探针,";
                $array['resources'][$key]['ability']=$ability;
            }
            if ($value['ability'] & 0x00000040) {
                $ability .= "农业传感,";
                $array['resources'][$key]['ability']=$ability;
            }
            $ability='';
        }
        if($return->result == '0'){
            $pageid = I('post.pageid');
            $pagecount = I('post.pagecount');
            // 根据页码和条数截取数据
            $array['resources'] = array_slice($array['resources'],($pageid-1)*$pagecount,$pagecount);
            $array['page'] = [   // 分页参数
                'totnum'=>count($array['resources']),   // 总条数
                'pageid'=>$pageid,   // 当前页数
                'pagecount'=>$pagecount,   // 显示条数
            ];
        }
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 获取资源组列表json格式
     */
    public function device_group_json()
    {
        $param = I('post.');
        $param['restype']= 0; //获取资源组
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ResourcesLists, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 添加资源组
     */
    public function AddResources_post()
    {
        $param = I('post.');
        $param['parentid'] = $param['parentid'] ? $param['parentid'] : 0;
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::AddResources, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 修改资源组
     */
    public function ModifyResources_post()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ModifyResources, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    public function device_group_xq()
    {
        $this->display();
    }
  
    /**
     * 设置用户视频资源权限
     */
    public function SetUserVideoPermissions()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::SetUserVideoPermissions, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 获取资源授权用户
     */
    public function getAuthorizationUser()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::getAuthorizationUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 删除资源
     */
    public function ModifyAuthorizationUser()
    {
        $param = I('post.');
        $param['groupid']=0;//移除
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ModifyAuthorizationUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }


}